// Decompiled by GOOSIN CO,LTD Copyright 2010 Pavel Kouznetsov.
// home page: http://www.Goosin.net
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   change_ok.java

package purchase.purchase;

import include.nseer_db.nseer_db_backup1;
import java.io.IOException;
import java.sql.ResultSet;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.servlet.jsp.JspFactory;
import validata.ValidataNumber;

public class change_ok extends HttpServlet
{

	public change_ok()
	{
	}

	public synchronized void service(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException
	{
		HttpSession dbSession = request.getSession();
		JspFactory _jspxFactory = JspFactory.getDefaultFactory();
		javax.servlet.jsp.PageContext pageContext = _jspxFactory.getPageContext(this, request, response, "", true, 8192, true);
		javax.servlet.ServletContext dbApplication = dbSession.getServletContext();
		ValidataNumber validata = new ValidataNumber();
		try
		{
			nseer_db_backup1 purchase_db = new nseer_db_backup1(dbApplication);
			if (purchase_db.conn((String)dbSession.getAttribute("unit_db_name")))
			{
				String purchase_id = request.getParameter("purchase_id");
				String provider_id = request.getParameter("provider_id");
				String provider_name = request.getParameter("provider_name");
				String demand_price1 = request.getParameter("demand_price");
				String changer = request.getParameter("changer");
				String changer_id = request.getParameter("changer_id");
				String change_time = request.getParameter("change_time");
				try
				{
					int p = 0;
					if (!validata.validata(demand_price1))
						p++;
					if (p == 0)
					{
						Double demand_price = Double.valueOf(Double.parseDouble(demand_price1));
						String sqll = (new StringBuilder("select demand_price,price_percent,demand_amount,demand_cost_price_sum from purchase_details where purchase_id='")).append(purchase_id).append("' and provider_id='").append(provider_id).append("'").toString();
						ResultSet rs = purchase_db.executeQuery(sqll);
						if (rs.next())
						{
							Double provider_price = Double.valueOf(rs.getDouble("demand_price"));
							Double price_percent = Double.valueOf(rs.getDouble("price_percent"));
							if (demand_price.doubleValue() <= provider_price.doubleValue() * price_percent.doubleValue())
							{
								Double demand_amount = Double.valueOf(rs.getDouble("demand_amount"));
								Double demand_cost_price_sum = Double.valueOf(rs.getDouble("demand_cost_price_sum"));
								Double price_sum_temp = Double.valueOf(demand_amount.doubleValue() * demand_price.doubleValue());
								sqll = (new StringBuilder("update purchase_details set demand_price='")).append(demand_price).append("',demand_cost_price_sum='").append(price_sum_temp).append("',changer='").append(changer).append("',changer_id='").append(changer_id).append("',change_time='").append(change_time).append("',change_tag='1' where purchase_id='").append(purchase_id).append("' and provider_id='").append(provider_id).append("'").toString();
								purchase_db.executeUpdate(sqll);
								Double post = Double.valueOf(price_sum_temp.doubleValue() - demand_cost_price_sum.doubleValue());
								sqll = (new StringBuilder("select demand_cost_price_sum,demand_amount from purchase_purchase where purchase_id='")).append(purchase_id).append("'").toString();
								rs = purchase_db.executeQuery(sqll);
								if (rs.next())
								{
									Double price_sum_main = Double.valueOf(rs.getDouble("demand_cost_price_sum"));
									Double demand_amount_main = Double.valueOf(rs.getDouble("demand_amount"));
									Double price_sum_main_temp = Double.valueOf(price_sum_main.doubleValue() + post.doubleValue());
									Double demand_price_main = Double.valueOf(price_sum_main_temp.doubleValue() / demand_amount_main.doubleValue());
									sqll = (new StringBuilder("update purchase_purchase set demand_price='")).append(demand_price_main).append("',demand_cost_price_sum='").append(price_sum_main_temp).append("' where purchase_id='").append(purchase_id).append("'").toString();
									purchase_db.executeUpdate(sqll);
								}
								sqll = (new StringBuilder("update stock_gather set cost_price='")).append(demand_price).append("',cost_price_sum='").append(price_sum_temp).append("' where reasonexact='").append(purchase_id).append("' and reasonexact_details='").append(provider_name).append("'").toString();
								purchase_db.executeUpdate(sqll);
								sqll = (new StringBuilder("select gather_id from stock_gather where reasonexact='")).append(purchase_id).append("' and reasonexact_details='").append(provider_name).append("'").toString();
								rs = purchase_db.executeQuery(sqll);
								if (rs.next())
								{
									sqll = (new StringBuilder("update stock_gather_details set cost_price='")).append(demand_price).append("',subtotal='").append(price_sum_temp).append("' where gather_id='").append(rs.getString("gather_id")).append("'").toString();
									purchase_db.executeUpdate(sqll);
								}
								sqll = (new StringBuilder("update fund_fund set qcs_dealwith_tag='1',demand_cost_price_sum='")).append(price_sum_temp).append("' where reasonexact='").append(purchase_id).append("' and funder_id='").append(provider_id).append("'").toString();
								purchase_db.executeUpdate(sqll);
								response.sendRedirect("purchase/purchase/change_ok.jsp?finished_tag=0");
							} else
							{
								response.sendRedirect((new StringBuilder("purchase/purchase/change_ok_b.jsp?purchase_id=")).append(purchase_id).append("&provider_id=").append(provider_id).toString());
							}
						}
					} else
					{
						response.sendRedirect((new StringBuilder("purchase/purchase/change_ok_a.jsp?purchase_id=")).append(purchase_id).append("&provider_id=").append(provider_id).toString());
					}
				}
				catch (Exception ex)
				{
					ex.printStackTrace();
					response.sendRedirect("purchase/purchase/change_ok.jsp?finished_tag=1");
				}
			} else
			{
				response.sendRedirect("purchase/purchase/change_ok.jsp?finished_tag=2");
			}
			purchase_db.commit();
			purchase_db.close();
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
	}
}
